<?php

require_once "../util.php";
require_once "../config.php";

connect_db_mute();

$tag = mysql_real_escape_string($_GET['tag']);

$sql = sprintf("select email from sina_user where tags like '%%%s%%'", $tag);
$ret = sql_or_die($sql);

$candidates = array();

$today = date('Y-m-d');

while ($account = mysql_fetch_assoc($ret)) {
  $email = $account["email"];

  $sql = sprintf("select * from user_statistic where user like '%%%s%%' and collect_date = '%s'", $email, $today);

  $cur_ret = sql_or_die($sql);
  $cur = array();

  while ($record = mysql_fetch_assoc($cur_ret)) {
    if (count($cur) == 0 || $cur['follow_count'] < $record['follow_count']) {

     $cur = $record;
    }

  }

  if (count($cur) > 0) {
    $candidates[] = $cur;
  }
}

echo json_encode($candidates);

?>
